import * as step from '@flow-step/step-toolkit'
import process from 'process'
import {gitMerge} from './merge'
import {masking} from './util'

async function runStep(): Promise<void> {

    let envs = process.env.PATH
    if(process.platform == 'win32' && envs != undefined && !envs.includes('Git')){
        step.error('Please install Git or set global envs first!')
        throw new Error('check git failed!')
    }

    const CLEAN_CACHE = process.env.CLEAN_CACHE || 'false';

    step.info('PIPELINE_ID=' + Number(process.env.PIPELINE_ID));
    step.info('PIPELINE_NAME=' + process.env.PIPELINE_NAME as string);
    step.info('BUILD_NUMBER=' + Number(process.env.BUILD_NUMBER));
    step.info('EMPLOYEE_ID=' + process.env.operator);
    step.info('WORK_SPACE=' + process.env.WORK_SPACE as string);
    step.info('PROJECT_DIR=' + process.env.PROJECT_DIR as string);
    step.info('PLUGIN_DIR=' + process.env.PLUGIN_DIR as string);
    step.info('BUILD_JOB_ID=' + Number(process.env.BUILD_JOB_ID));
    step.info('CLEAN_CACHE=' + CLEAN_CACHE);
    step.info('DELETE_ORIGIN_BRANCH=' + process.env.DELETE_ORIGIN_BRANCH);
    step.info('DELETE_MATCHED_BRANCH=' + process.env.DELETE_MATCHED_BRANCH);
    step.info('BRANCH_FILTER=' + process.env.BRANCH_FILTER);

    step.exportEnvVar("CLEAN_CACHE", CLEAN_CACHE)

    await gitMerge();


}

runStep()
    .then(function () {
        step.success('run step GitMerge successfully!')
    })
    .catch(function (err: Error) {
        step.error(masking(err.message))
        process.exit(-1)
    })
